package info.douyi.dps.controller;

import info.douyi.dps.service.QiniuService;
import info.douyi.dps.utils.Result;
import jakarta.annotation.security.PermitAll;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/qiniu")
@CrossOrigin(origins = "*", allowCredentials = "false") // ← 关键
@Slf4j
public class QiniuController {

    @Autowired
    private QiniuService qiniuService;


    @RequestMapping(value="/uploadImg", method= RequestMethod.POST)
    @PermitAll
    public Result uploadImg(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        Result success = Result.success();

        if(file.isEmpty()) {
            success.setCode(400);
            success.setMessage("文件不能为空");
            return success;
        }

        try {
            String fileUrl=qiniuService.saveImage(file);
            success.setCode(200);
            success.setMessage("文件上传成功");
            success.setData(fileUrl);

            return success;
        } catch (IOException e) {
            e.printStackTrace();
        }
        success.setCode(500);
        success.setMessage("文件上传发生异常！");
        return success;
    }
}


